package com.cmrx.model.service.impl;

import com.cmrx.model.dao.CommonPictureDao;
import com.cmrx.model.dao.PictureThumbnailDao;
import com.cmrx.model.dao.SceneBioEvidenceDao;
import com.cmrx.model.entity.CommonPicture;
import com.cmrx.model.entity.CommonPictureThumbnail;
import com.cmrx.model.entity.SceneBioEvidence;
import com.cmrx.model.entity.User;
import com.cmrx.model.service.CommonPictureServer;
import com.cmrx.model.service.SceneBioEvidenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class SceneBioEvidenceServiceImpl implements SceneBioEvidenceService {
    @Autowired
    private SceneBioEvidenceDao sceneBioEvidenceDao;

    @Autowired
    private CommonPictureDao commonPictureDao;

    @Autowired
    private PictureThumbnailDao pictureThumbnailDao;

    @Autowired
    private CommonPictureServer commonPictureServer;

    @Override
    public List<SceneBioEvidence> queryByInvestigationId(String investigationId) {
        List<SceneBioEvidence> bios = sceneBioEvidenceDao.queryByInvestigationId(investigationId);
        List<SceneBioEvidence> list = new ArrayList<>();
        for(SceneBioEvidence bio:bios) {
            CommonPictureThumbnail com = pictureThumbnailDao.queryByPictureId(bio.getEvidencePhotoId());
            bio.setContent(com.getContent());
            list.add(bio);
        }
        return list;
    }

    //根据现勘id获取生物物证统计
    @Override
    public Integer queryCount(String investigationId) {
        return sceneBioEvidenceDao.queryCount(investigationId);
    }

    //根据id查找生物物证信息
    @Override
    public SceneBioEvidence queryById(String id) {
        SceneBioEvidence bio = sceneBioEvidenceDao.queryById(id);
        if(bio!=null) {
            CommonPicture com = commonPictureDao.queryById(bio.getEvidencePhotoId());
            bio.setContent(com.getContent());
        }
        return bio;
    }

    //添加生物物证信息
    @Override
    public String addBioEvidence(SceneBioEvidence bioEvidence, User user,CommonPicture commonPicture) {
        commonPicture.setInvestigationId(bioEvidence.getInvestigationId());
        commonPicture.setContent(bioEvidence.getContent());
        commonPicture.setCategory("8");
        String pictureId = commonPictureServer.AddPicture(user,commonPicture);
        bioEvidence.setId(UUID.randomUUID().toString().replaceAll("-",""));
        bioEvidence.setCreateUserId(user.getUserid());
        bioEvidence.setCreateUser(user.getUserName());
        bioEvidence.setEvidencePhotoId(pictureId);
        int serialNo = 1;
        Integer no = sceneBioEvidenceDao.queryMaxNo(bioEvidence.getInvestigationId());
        if(no!=null){
            serialNo = no+1;
        }
        bioEvidence.setSerialNo(serialNo+"");
        int count = sceneBioEvidenceDao.addBioEvidence(bioEvidence);
        if(count>0) {
            return bioEvidence.getId();
        }else{
            return "";
        }
    }

    //根据id修改生物物证信息
    @Override
    public int updateById(SceneBioEvidence bioEvidence, User user) {
        SceneBioEvidence bio = sceneBioEvidenceDao.queryById(bioEvidence.getId());
       commonPictureServer.updatePicture(user,bio.getEvidencePhotoId(),bioEvidence.getContent());
        bioEvidence.setUpdateUser(user.getUserName());
        int count = sceneBioEvidenceDao.updateById(bioEvidence);
        return count;
    }

    //根据id删除生物物证信息
    @Override
    public int deleteById(String id) {
        SceneBioEvidence bio = sceneBioEvidenceDao.queryById(id);
        if(bio!=null) {
            pictureThumbnailDao.deleteById(bio.getEvidencePhotoId());
            commonPictureDao.deleteById(bio.getEvidencePhotoId());
        }
        int result = sceneBioEvidenceDao.deleteById(id);
        return result;
    }
}
